/********************************************/
// Creation History
// --------------------------------------------------------------
//	Company:			HTHK
//	NAME:			EmployeeSearch.java
// Title   :			find Employee Records, return one or more records
//	FUNCTION:
//	BY:				Beyes Xia
//	DATE:			2004-1-13 15:48
// --------------------------------------------------------------
/*********************************************/
/*--------------------------------------------------------------
 Amend record
 Date          Version         Author          Description
 06/07/2004    1.0             Beyes           For log 1304.
 ---------------------------------------------------------------*/
package tss.employee;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import tss.common.DBServlet2;

import com.ffpc.struts.StrutsMessage;
import com.util.JdbcUtil;

/**
 * A Class class.
 * <P>
 * 
 * @author HTHK
 */
public class EmployeeSearch extends DBServlet2 {
	private Logger logger = Logger.getLogger(EmployeeSearch.class);
	@Override
	protected String perform(HttpServletRequest request,
			HttpServletResponse response, StrutsMessage message) {
		String staffName = "";
		String position = "";
		String joinDateFrom = "";
		String joinDateTo = "";
		String department = "";
		String costCenter = "";
		String employeeType = "";

		if (request.getParameter("staff_name") != null)
			staffName = request.getParameter("staff_name");
		if (request.getParameter("position") != null)
			position = request.getParameter("position");
		if (request.getParameter("join_date_from") != null)
			joinDateFrom = request.getParameter("join_date_from");
		if (request.getParameter("join_date_to") != null)
			joinDateTo = request.getParameter("join_date_to");
		if (request.getParameter("department") != null)
			department = request.getParameter("department");
		if (request.getParameter("cost_center") != null)
			costCenter = request.getParameter("cost_center");
		if (request.getParameter("employee_type") != null)
			employeeType = request.getParameter("employee_type");

		HttpSession session = request.getSession();
		session.setAttribute("s_staff_name", staffName);
		session.setAttribute("s_position", position);
		session.setAttribute("s_join_date_from", joinDateFrom);
		session.setAttribute("s_join_date_to", joinDateTo);
		session.setAttribute("s_department", department);
		session.setAttribute("s_cost_center", costCenter);
		session.setAttribute("s_employee_type", employeeType);

		// logger.debug("staffName="+staffName);
		// logger.debug("position="+position);
		// logger.debug("joinDateFrom="+joinDateFrom);
		// logger.debug("joinDateTo="+joinDateTo);
		// logger.debug("department="+department);
		// logger.debug("costCenter="+costCenter);
		// logger.debug("employeeType="+employeeType);

		// ---------------- Add by Beyes Xia for log 1304 2004-07-06
		// ----------------------------
		String isOneRecord = "Y";
		if (request.getParameter("one_record") != null)
			isOneRecord = request.getParameter("one_record");
		// ---------------- Add by Beyes Xia
		// ---------------------------------------

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		Vector employees = new Vector();
		try {
			con = this.getConnection();
			stmt = con.createStatement();

			String sql = "select STAFF_NO,SURNAME,FIRST_NAME,ALIAS,to_char(JOIN_DATE,'YYYY/MM/DD'),to_char(LAST_DATE,'YYYY/MM/DD'),DEPARTMENT_CODE,COST_CENTER,POSITION,EMPLOYMENT_TYPE,IMPORT_DT "
					+ "from tss.tss_c_employee " + "where 1=1 ";
			if (staffName != null && staffName.length() != 0) {
				sql += "and staff_no='" + staffName + "' ";
			}
			if (position != null && position.length() != 0) {
				sql += "and position='" + position + "' ";
			}
			if (joinDateFrom != null && joinDateFrom.length() != 0) {
				sql += "and join_date>=TO_DATE('" + joinDateFrom
						+ "','YYYY-MM-DD') ";
			}
			if (joinDateTo != null && joinDateTo.length() != 0) {
				sql += "and join_date<=TO_DATE('" + joinDateTo
						+ "','YYYY-MM-DD') ";
			}
			if (department != null && department.length() != 0) {
				sql += "and department_code='" + department + "' ";
			}
			if (costCenter != null && costCenter.length() != 0) {
				sql += "and cost_center='" + costCenter + "' ";
			}
			if (employeeType != null && employeeType.length() != 0) {
				sql += "and employee_type='" + employeeType + "' ";
			}
			sql += "order by ALIAS";
			// logger.debug("SQL is : " +sql);
			rs = stmt.executeQuery(sql);

			int count = 0;
			while (rs.next()) {
				String[] employee = new String[11];

				employee[0] = rs.getString(1);
				employee[1] = rs.getString(2);
				employee[2] = rs.getString(3);
				employee[3] = rs.getString(4);
				employee[4] = rs.getString(5);
				employee[5] = rs.getString(6);
				employee[6] = rs.getString(7);
				employee[7] = rs.getString(8);
				employee[8] = rs.getString(9);
				employee[9] = rs.getString(10);
				employee[10] = rs.getString(11);

				employees.addElement(employee);
				count++;
			}
			// logger.debug("count:"+count);
			if (employees.isEmpty()) {
				message.setMessage("No record, please input another criteria.");
				return "tss/employee/EmployeeSearch.jsp";
			} else if (employees.size() == 1) {
				String[] employee = new String[11];
				employee = (String[]) employees.get(0);
				request.setAttribute("employee", employee);
				return "tss/employee/Employee.jsp?isOneRecord=" + isOneRecord;
			} else {
				request.setAttribute("employees", employees);
				return "tss/employee/EmployeeList.jsp";
			}

		} catch (Exception e) {
			// logger.info("Execute SQL error");
			logger.error(e,e);
			message.setMessage("No record, please input another criteria.");
			return "tss/employee/EmployeeSearch.jsp";
		} finally {
			JdbcUtil.release(stmt, rs);
		}
	}
}
